const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

app.use(async(ctx)=>{
    if(ctx.url === '/index'){
        ctx.cookies.set(
            'myname', 'qishon',{
                domain: '127.0.0.1',    //在访问127.0.0.1的时候生效
                path: '/index', //在访问/index的时候生效
                maxAge: 1000*60*60, //cookie的最大时间
                expires: new Date('2018-12-31'),    //失效时间
                httpOnly: false,    //不仅仅是http的时候生效
                overwrite: false    //不允许重写
            }
        );
        ctx.body = 'Cookie is ok';
    }else{
        if(ctx.cookies.get('myname')){
            ctx.body = ctx.cookies.get('myname');
        }else{
            ctx.body = 'cookie is not';
        }
    }
})

app.listen(3000, ()=>{
    console.log('starting at port 3000');
})